/**
 * 2016年10月22日
 */
package assignments;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;

import tsc_algorithms.elastic_ensemble.DTW1NN;
import weka.classifiers.Classifier;
import weka.core.Instance;
import weka.core.Instances;

/**
 * @author Alex
 *
 */
public class RunClassifier {
	static class Counter{
		public int correct;
		public int total;
		public String toString(){
			return String.format("Total:%d,Correct:%d,Accuracy:%f", total,correct,(double)correct/total);
		}
		public Counter aggregate(Counter c){
			this.correct += c.correct;
			this.total += c.total;
			return this;
		}
		
	}
	public static void main(String[] args) throws Exception {
		Instances insts = Util.read( new File("D:\\wekadataset\\timeseries\\datasets\\ARFF\\Gun_Point_TRAIN.arff"));
		insts.setClassIndex(insts.numAttributes()-1);
		Classifier classifier = new DTW1NN();
		classifier.buildClassifier(insts);
		Instances test = Util.read("D:\\wekadataset\\timeseries\\datasets\\ARFF\\Gun_Point_TEST.arff");
		test.setClassIndex(insts.numAttributes()-1);
		Util.<Instances>forEach((fileDs)->{
			try {
				//classifier.buildClassifier(fileDs);
				Counter  c = fileDs.stream().reduce(new Counter(), (Counter res,Instance in)->{
					res.total++;
					try {
						int dis = (int)classifier.classifyInstance(in);
						if(dis==(int)in.classValue()){
							res.correct++;
						}
					} catch (Exception e) {e.printStackTrace();}
					return res;
				}, (Counter a,Counter b)->{
					return a.aggregate(b);
					});
				System.out.println(c);
			} catch (Exception e) {
				e.printStackTrace();
			}		
		},test);
		
		
		
		
		
//		Util.forEachArffFileInstances((fileDs)->{
//			try {
//				classifier.buildClassifier(fileDs);
//				Counter  c = fileDs.stream().reduce(new Counter(), (Counter res,Instance in)->{
//					return res;
//				}, (Counter a,Counter b)->{
//					a.aggregate(b);
//					return a;
//					});
//				System.out.println(c);
//			} catch (Exception e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			
//		});
		
		
		
		
	}
	public static void main1(String args[]) throws FileNotFoundException, IOException{
//		BiFunction<Instances, Classifier, Counter> task = (ds,classifier)->{
//			try {
//				classifier.buildClassifier(ds);
//			} catch (Exception e1) {
//				// TODO Auto-generated catch block
//				e1.printStackTrace();
//			}
//			
//			//ds.forEach(classifyAndStats);
//			return new Counter();
//		};
//		Counter c = new Counter();
		Util.forEachArffFileInstances((dataSet)->{
			
//			dataSet.stream().reduce(identity, accumulator, combiner)
//			dataSet.stream()
			//dataSet.stream().reduce(identity, accumulator, combiner)			
		});
	}

//	Consumer<Instance> classifyAndStats = (i)->{
//		counter.total++;
//		try {
//			if((int)dtw1nn.classifyInstance(i)==(int)i.classValue()){
//				counter.correct++;
//			}
//		} catch (Exception e){e.printStackTrace();} 
//	};
	
}
